node.js中fs模块 您所在的位置:网站首页 node 引入fs模块为空 node.js中fs模块

node.js中fs模块

2024-02-23 13:11| 来源: 网络整理| 查看: 265

在node服务中,在对文件或者文件夹进行读写操作时,一般会使用fs模块,fs模块提供的操作方法,会分异步操作和同步操作。

fs模块的引用

1.在项目中使用npm安装npm模块,在终端中使用npm install fs

2.在资源文件中使用require进行引用 

var fs = require('fs'); fs的文件删除操作 unlink('文件路径',function(err){}) 和unlinkSync('文件路径'), 1)只能删除文件,不能删除文件夹 2)不能删除不存在的文件 // 异步删除文件 删除文件 不能删除不存在的文件 fs.unlink('./upload/fsTest.js',function (err) { if (err) { console.log(err+'错误信息'); // throw err; }; console.log('删除成功'); }); //同步删除文件 不能删除 不存在的文件 fs.unlinkSync('./upload');

fs创建文件夹操作  函数mkdir('文件夹名',function(err){}), mkdirSync('文件夹名'),不能创建已经存在的文件夹 //创建文件夹 fs.mkdir('testMkdir',function(err){ if (err) { console.log(err+'错误信息'); //throw err; }; console.log('文件夹创建成功'); }); //同步创建文件夹 fs.mkdirSync('./install');

创建已经存在的文件夹,会报错,告诉你 EEXIST: file already exists, mkdir './install'

fs删除文件夹操作 rmdir('文件夹',function(err){})和rmdirSync('文件夹路径') 这个只能删除空的文件夹。 //1.只能删除文件夹 而且必须是空的文件夹 2.不能删除文件 fs.rmdir('./testMkdir',function(err){ if (err) { console.log(err+'错误'); }else{ console.log('删除成功'); }; }); //同步删除 不能删除不存在的 只能删除文件夹 fs.rmdirSync('./install');

fs读取文件 异步读取方式readFile('文件路径',function(err,data){}); 同步读取方式 var fileData = readFile('文件路径'); 同步读取文件,会出现卡顿。

//读取文件 fs.readFile(filePath,function(err,data){ if (err) { console.log(err+'操作错误'); }; console.log(data.toString()+'文件数据'); var obj = JSON.parse(data); console.log(obj.key); }); var fileData = fs.readFileSync(filePath); var obj = JSON.parse(fileData); console.log(obj.key+'同步读取');

读取获取的数据格式问题:读取出来的数据格式是Buffer类型,具体判断方式可以使用Buffer.isBuffer(判断对象) 返回true 代表着 是Buffer对象。

fs文件写入  writeFile(path,data,callback),path 写入的路径,data写入的数据,必须要保证是字符串形式,callback 回调函数 //写入文件 fs.writeFile(file, data[, options], callback) 请确保data是string类型 fs.writeFile(writeFilePath,JSON.stringify(obj),function(err){ if (err) { console.log('写入失败'); }else{ console.log('写入成功'); }; }); //同步写入 fs.writeFileSync(writeFilePath,fileData.toString()); fs以追加的方式写入 appendFile(file,data,callback),file ->被追加写入的文件 data->追加的数据 字符串类型 callback ->回调 //以追加的方式写入 var appendFilePath = path.join(__dirname,'./01.txt'); fs.appendFile(appendFilePath,'追加写入数据',function(err){ if (err) { console.log('写入失败'); }else{ console.log('写入成功'); }; }); fs.appendFileSync(appendFilePath,'aaa'); fs修改文件名rename(旧名,新名,callball) //更改名字 fs.rename('./01.txt','01.docx',function(err){ if (err) { console.log('修改失败'); }else{ console.log('修改成功'); }; fs判断文件是否存在 异步exists(path,callback) 同步existsSync(path,callback)  //判断文件是否存在 fs.exists('./01.txt',function(exists){ if (exists) { console.log('文件存在'); }else{ console.log('文件不存在'); }; }); var exist = fs.existsSync('./01.docx'); if (exist) { console.log('01.docx文件存在'); }; fs读取一个文件夹的内容 不会逐级读取 只读取当前目录的 //读取一个目录 fs.readdir(path.join(__dirname,'./www/'),function(err,files){ if (err) { console.log('读取失败'); }else{ console.log(files+'读取文件路径'); }; }); var filesArr = fs.readdirSync(path.join(__dirname,'./www/Imgs')); console.log('同步获取'+filesArr);

 

学习参考博客:

https://www.jianshu.com/p/5966d6331abc

https://www.jianshu.com/p/5966d6331abc

https://www.jianshu.com/p/117f12a72abd

https://www.jianshu.com/p/c74769c456f1



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有